import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbfdmenu } from '../../../../atoms/db';

const logger = anylogger('pages/api/admin/menu/query.api');

export type Result = ITbfdmenu[];

export type Message = {
	pid: string;
}

/**
 * 查询子菜单
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	try {
		logger.debug('msg query:', req.query);
		const { pid } = req.query as Message;
		if (!pid) {
			res.status(200).json([]);
			return;
		}
		const d = await tbfdmenu()
			.select('*')
			.where({
				pid
			})
			.orderBy('weight', 'desc')
			.orderBy('name', 'asc');
		const data = d.map((it) => {
			return {
				...it
			};
		});
		res.status(200).json(data);
	} catch (error) {
		logger.trace(error);
		res.status(200).json([]);
	}
});

export const config = {} as PageConfig;

export default handler;
